package com.sleepless.my.config;

import com.sleepless.my.entity.UsersEntity;
import com.sleepless.my.repository.UsersRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Slf4j
@Component
public class JwtUserDetailService implements UserDetailsService {
    @Resource
    UsersRepository usersRespository;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UsersEntity user = usersRespository.findByusername(userName);
        // 构建 UserDetails 对象
        return new CustomUserDetails(
                userName,
                user.getPassword(),
                new ArrayList<>(),  // 这里可以设置用户的权限信息，可以根据实际情况设置
                user.getRole(),
                user.getUuid()
        );
    }
}
